package com.itheima.ai.tools;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.ai.dto.CourseQueryDto;
import com.itheima.ai.memory.InMysqlChatMemory;
import com.itheima.ai.pojo.Course;
import com.itheima.ai.pojo.CourseReservation;
import com.itheima.ai.pojo.School;
import com.itheima.ai.service.ICourseReservationService;
import com.itheima.ai.service.ICourseService;
import com.itheima.ai.service.ISchoolService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CourseTools {


    @Autowired
    private ICourseService courseService;

    @Autowired
    private ISchoolService schoolService;

    @Autowired
    ICourseReservationService courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> getCourseList(@ToolParam(required = false, description = "课程查询条件") CourseQueryDto dto) {
        LambdaQueryWrapper<Course> wrapper = Wrappers.lambdaQuery();
        wrapper.le(dto.getEdu() != null, Course::getEdu, dto.getEdu());
        wrapper.eq(dto.getType() != null, Course::getType, dto.getType());
        if (!CollectionUtils.isEmpty(dto.getSorts())) {
            dto.getSorts().forEach(sort -> {
                wrapper.orderBy(true, sort.getAsc(), Course::getPrice);
            });
        }
        return courseService.list(wrapper);
    }


    @Tool(description = "查询校区列表")
    public List<School> querySchoolList() {
        List<School> list = schoolService.list();
        return list;
    }


    @Tool(description = "新增试听预约单,并返回预约单号")
    public String addReservation(@ToolParam(required = true, description = "课程名称") String name,
                                 @ToolParam(required = true, description = "学生姓名") String studentName,
                                 @ToolParam(required = true, description = "联系方式") String contactInfo,
                                 @ToolParam(required = true, description = "校区名称") String schoolName,
                                 @ToolParam(required = false, description = "备注") String remark) {
        CourseReservation courseReservation = CourseReservation.builder()
                .course(name)
                .school(schoolName)
                .studentName(studentName)
                .contactInfo(contactInfo)
                .remark(remark)
                .build();
        courseReservationService.save(courseReservation);
        return courseReservation.getId().toString();
    }


}
